// 地区select框
<style lang="less" scoped>
</style>
<template>
  <Select @on-change="sendData"
          clearable
          filterable
          :multiple="multipleType"
          v-model="area"
          placeholder="请选择行政区域">
    <Option v-for="(item,index) in areaList"
            :value="index"
            :key="item.name">{{ item.name }}</Option>
  </Select>
</template>
<script>
import { ajaxPost } from 'global/axios';
import storage from 'good-storage';
export default {
  props: {
    multipleType: {
      type: Boolean,
      default() {
        return false;
      },
    },
  },
  data() {
    return {
      urlAreaList: '/law/city/list',
      area: '',
      areaList: [],
    };
  },
  methods: {
    getArea() {
      let params = {
        data: {
          basis: 2,
          fieldName: ['name', 'level'],
          searchValue: { level: '3|#|lte' },
          order: { level: 1 },
        },
        url: this.urlAreaList,
        token: storage.session.get('token'),
        success: res => {
          this.$Loading.finish();
          this.areaList = res.data.result.result;
        },
      };
      ajaxPost(params);
    },
    sendData(index) {
      if (this.multipleType) { // 多选
        let areaArr = [];
        if (this.area && this.area.length > 0) {
          this.area.map(item => {
            areaArr.push(this.areaList[item]);
          })
        }
        this.$emit('getData', areaArr);
      } else {
        this.$emit('getData', this.areaList[index]);
      }
    },
  },
  mounted() {
    this.getArea();
  },
};
</script>

